<?php
if(!class_exists('group'))
{
	include('group.class.php');
}
/**
 * 组用户类
 *
 */
class group_user extends group 
{
	public $gu_table;
	
	function group_user()
	{
		parent::group();
		$this->gu_table = DB_PRE.'group_user';
	}
	
	function get_group_user_info($gid,$page,$pagesize)
	{
		$start = ($page - 1) * $pagesize;
		$this->db->prepare('select count(u_id) as nums from '.$this->gu_table.' where g_id=? and status=?');
		$this->db->set_string($gid);
		$this->db->set_int(STATUS_NORMAL);
		$res_count = $this->db->execute(true);
		$count = intval($res_count['nums']);
		if($count==0)
		{
			$this->pages = '';
			return false;
		}
		$this->db->prepare('select r.u_id,r.username,r.inputtime,a.username,a.truename from '.$this->gu_table.' as r join '.$this->utable.' as a on a.userid=r.u_id and a.status>0 and r.status>0 where r.g_id=? order by r.inputtime asc limit ?,?');
		$this->db->set_string($gid);
		$this->db->set_int($start);
		$this->db->set_int($pagesize);
		$list = $this->db->execute();
		$this->pages = pages($count,$page,$pagesize);
		return $list;
	}
	
	function check_user_in_group($uid,$gid)
	{
		$this->db->prepare('select count(*) as nums from '.$this->gu_table.' where status=? and g_id=? and u_id=?');
		$this->db->set_int(STATUS_NORMAL);
		$this->db->set_string($gid);
		$this->db->set_string($uid);
		$res = $this->db->execute(true);
		$count = intval($res['nums']);
		return $count > 0 ? true : false;
	}
	
	function add_user($uid,$gid)
	{
		$params = array(
			'g_id'	=>	$gid,
			'u_id'	=>	$uid,
			'username'	=>	$this->current_user['username'],
			'inputtime'	=>	time()
		);
		$res = $this->db->insert($this->gu_table,$params);
		return $res;
	}
	
	function del_user($uid,$gid)
	{
        return $this->db->fake_delete($this->gu_table, array('g_id'=>$gid,'u_id'=>$uid));
	}
	
	function get_user_groups($uid)
	{
		$this->db->prepare('select g_id from '.$this->gu_table.' where status=? and u_id=?');
		$this->db->set_int(STATUS_NORMAL);
		$this->db->set_string($uid);
		$res = $this->db->execute();
		$list = array();
		if(is_array($res))
		{
			foreach ($res as $r)
			{
				$list[] = $r['g_id'];
			}
		}
		return $list;
	}
	
}

/**
 * 组角色类
 *
 */
class group_user_role extends group 
{
	public $gur_table;
	
	function group_user_role()
	{
		parent::group();
		$this->gur_table = DB_PRE.'group_user_role';
	}
	
	/**
	 * 获得用户在组中自定义的角色
	 *
	 * @param int $uid
	 * @param int $gid
	 * @return array
	 */
	function get_user_group_role($uid,$gid)
	{
		$this->db->prepare('select r_id from '.$this->gur_table.' where status=? and g_id=? and u_id=?');
		$this->db->set_int(STATUS_NORMAL);
		$this->db->set_string($gid);
		$this->db->set_string($uid);
		$res = $this->db->execute();
		$list = array();
		if(is_array($res) && sizeof($res) > 0)
		{
			foreach ($res as $v)
			{
				$list[] = $v['r_id'];
			}
		}
		return $list;
	}
	
	/**
	 * 增加用户在指定组中的自定义角色
	 *
	 * @param int $uid
	 * @param int $gid
	 * @param int $rid
	 * @return bool
	 */
	function add_user_group_role($uid,$gid,$rid)
	{
		$params = array(
			'u_id'	=>	$uid,
			'g_id'	=>	$gid,
			'r_id'	=>	$rid
		);
		$res = $this->db->insert($this->gur_table,$params);
		return $res===false ? false : true;
	}
	
	/**
	 * 删除用户在指定组下的自定义角色
	 *
	 * @param int $uid
	 * @param int $gid
	 * @return bool
	 */
	function del_user_group_role($uid,$gid)
	{
        return $this->db->fake_delete($this->gur_table, array('u_id'=>$uid,'g_id'=>$gid));
	}
}

/**
 * 组角色权限
 *
 */
class group_role extends group 
{
	public $gr_table;
	
	function group_role()
	{
		parent::group();
		$this->gr_table = DB_PRE.'group_role_perm';
	}
	/**
	 * 添加组角色权限
	 *
	 * @param int $gid
	 * @param int $rid
	 * @param string $perms
	 * @return int or false
	 */
	function add_group_role($gid,$rid,$perms)
	{
		$params = array(
			'g_id'			=>	$gid,
			'roleid'		=>	$rid,
			'grp_permids'	=>	implode(',',$perms)
		);
		$res = $this->db->insert($this->gr_table,$params);
		return $res;
	}
	
	/**
	 * 删除组角色
	 *
	 * @param int $gid
	 * @param int $rid
	 * @return bool
	 */
	function delete_group_role($gid,$rid)
	{
        return $this->db->fake_delete($this->gr_table, array('g_id'=>$gid,'roleid'=>$rid));
	}
	
	/**
	 * 修改组角色权限
	 *
	 * @param int $gid
	 * @param int $rid
	 * @param array $perms
	 * @return bool
	 */
	function edit_group_role($gid,$rid,$perms)
	{
		$params = array(
			'grp_permids'=>implode(',',$perms),
		);
		$conditions = array(
			'g_id'			=>	$gid,
			'roleid'		=>	$rid,
		);
		$res = $this->db->simple_update($this->gr_table,$params,$conditions);
		return $res;
		
	}
	
	/**
	 * 获得指定组下的自定义角色权限
	 *
	 * @param int $gid
	 * @return array
	 */
	function get_group_roles($gid)
	{
		$this->db->prepare('select * from '.$this->gr_table.' where status=? and g_id=?');
		$this->db->set_int(STATUS_NORMAL);
		$this->db->set_string($gid);
		$res = $this->db->execute();
		$list = array();
		if(is_array($res) && sizeof($res) > 0)
		{
			foreach ($res as $role)
			{
				$list[$role['roleid']] = explode(',',$role['grp_permids']);
			}
		}
		return $list;
	}
}

/**
 * 组资源权限
 *
 */
class group_view extends group 
{
	public $gv_table;
	
	function group_view()
	{
		parent::group();
		$this->gv_table = DB_PRE.'group_view';
	}
	
	/**
	 * 添加组可查看的资源
	 *
	 * @param unknown_type $view_type
	 * @param unknown_type $vid
	 * @param unknown_type $gid
	 * @return unknown
	 */
	function add_view($view_type,$vid,$gid)
	{
		$params = array(
			'view_type'	=>	$view_type,
			'g_id'		=>	$gid,
			'vid'		=>	$vid
		);
		
		return $this->db->insert($this->gv_table,$params);
	}
	
	/**
	 * 删除组可查看的某一类型的资源
	 *
	 * @param unknown_type $gid
	 * @param unknown_type $view_type
	 * @return unknown
	 */
	function delete_group_view_type($gid,$view_type)
	{
        return $this->db->fake_delete($this->gv_table, array('view_type'=>$view_type,'g_id'=>$gid));
	}
	
	/**
	 * 更新组表中的资源内容
	 *
	 * @param int $gid
	 * @return bool
	 */
	function edit_group_view($gid)
	{
		$this->db->prepare('select * from '.$this->gv_table.' where status=? and g_id=?');
		$this->db->set_int(STATUS_NORMAL);
		$this->db->set_string($gid);
		$res = $this->db->execute();
		if(is_array($res))
		{
			$list = array();
			$final = array();
			foreach ($res as $v)
			{
				$list[$v['view_type']][] = $v['vid'];
			}
			
			foreach ($list as $k=>$v)
			{
				$final[] = $k.":".implode(',',$v);
			}
			$str = implode("|",$final);
		}
		else 
		{
			$str = '';
		}
		return $this->edit_group(array('view_content'=>$str),$gid);
	}
	
	/**
	 * 同步资源菜单
	 *
	 * @param int $gid
	 */
	function same_menu($gid,$view_type)
	{
		$source = array('category');
		if(!in_array($view_type,$source))
		{
			return true;
		}
		//获取所有具有关联关系的菜单
		$this->db->prepare('select id,keyid from '.DB_PRE.'menu where keyid!="" and keyid is not null and status=?');
		$this->db->set_int(STATUS_NORMAL);
		$menus = $this->db->execute();
		$mlist = array();
		if(is_array($menus) && sizeof($menus) > 0)
		{
			foreach ($menus as $menu)
			{
				$tmp = explode('_', $menu['keyid']);
				if(sizeof($tmp)==2)
				{
					$mlist[$tmp[0]][$menu['id']] = $tmp[1];
				}
			}
		}
		foreach ($source as $s)
		{
			//获取资源配置信息
			$this->db->prepare('select * from '.DB_PRE.'resource where status=? and view_type=?');
			$this->db->set_int(STATUS_NORMAL);
			$this->db->set_string($s);
			$conf = $this->db->execute(true);
			if(!isset($conf['resid']))
			{
				continue;
			}
			//删除有菜单和资源关联的菜单权限
			$this->db->prepare('update '.$this->gv_table." set status=? where g_id=? and view_type=? and vid in ('".implode("','",array_keys($mlist[$s]))."')");
			$this->db->set_string(STATUS_DEL);
			$this->db->set_string($gid);
			$this->db->set_string('menu');
			$res = $this->db->execute();
			
			//添加资源对应的菜单权限
			if($res===true)
			{
				$this->db->prepare('select * from '.$this->gv_table.' where status=? and g_id=? and view_type=?');
				$this->db->set_int(STATUS_NORMAL);
				$this->db->set_string($gid);
				$this->db->set_string($s);
				$list = $this->db->execute();
				if(is_array($list))
				{
					foreach ($list as $l)
					{
						$key = array_search($l['vid'], $mlist[$s]);
						if($key !== false) //如果资源有对应的菜单，将此菜单的权限插入到权限表中去
						{
							$data = array(
								'view_type'	=>	'menu',
								'g_id'		=>	$gid,
								'vid'		=>	$key
							);
							$res = $this->db->insert($this->gv_table,$data);
						}
					}
				}
			}
		}
		$cache = get_cache_obj();
		$cache->del('group_view_' . $gid . '_menu');
		return true;
	}
}
?>